package db

import (
	"time"
	"webcppcluster/config"
)

// InsertRegisterUser 插入注册用户
func InsertRegisterUser(phonenum, password, nick, sex, idcard string, now time.Time) error {
	sqlStr := `INSERT INTO user(userid, password, secpassword, nick, phonenum, sex, idcard, status, createtime) 
		VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)`
	
	_, err := config.DB.Exec(sqlStr, phonenum, password, config.DefaultSecPassword, 
		nick, phonenum, sex, idcard, config.UserStatusNormal, now)
	return err
}

// InitPackage 初始化背包
func InitPackage(userid int64, packageInfoList []PackageInfo, now time.Time) error {
	sqlStr := `INSERT INTO package(userid, propid, propnum, proptype, freshtime) VALUES(?, ?, ?, ?, ?)`
	
	for _, info := range packageInfoList {
		_, err := config.DB.Exec(sqlStr, userid, info.PropID, info.PropNum, info.PropType, now)
		if err != nil {
			return err
		}
	}
	return nil
}

// PackageInfo 背包信息
type PackageInfo struct {
	PropID   int
	PropNum  int
	PropType int
}

// GetPackageIdAndNum 获取背包道具ID和数量
func GetPackageIdAndNum(userid int64) ([]PackageItem, error) {
	sqlStr := `SELECT propid, propnum FROM package WHERE userid = ?`
	
	rows, err := config.DB.Query(sqlStr, userid)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var items []PackageItem
	for rows.Next() {
		var item PackageItem
		if err := rows.Scan(&item.PropID, &item.PropNum); err != nil {
			return nil, err
		}
		items = append(items, item)
	}
	return items, rows.Err()
}

// PackageItem 背包道具项
type PackageItem struct {
	PropID  int
	PropNum int
}

// UpdatePackageInfo 更新背包信息
func UpdatePackageInfo(userid int64, propid, propnum int, now time.Time) error {
	sqlStr := `UPDATE package SET propnum = ?, freshtime = ? WHERE userid = ? AND propid = ?`
	_, err := config.DB.Exec(sqlStr, propnum, now, userid, propid)
	return err
}

// UpdatePackageInfoBatch 批量更新背包信息
func UpdatePackageInfoBatch(updates []PackageUpdate) error {
	sqlStr := `UPDATE package SET propnum = ?, freshtime = ? WHERE userid = ? AND propid = ?`
	
	for _, update := range updates {
		_, err := config.DB.Exec(sqlStr, update.PropNum, update.FreshTime, update.UserID, update.PropID)
		if err != nil {
			return err
		}
	}
	return nil
}

// PackageUpdate 背包更新信息
type PackageUpdate struct {
	UserID    int64
	PropID    int
	PropNum   int
	FreshTime time.Time
}

// UpdateUserLastLoginTime 更新用户最后登录时间
func UpdateUserLastLoginTime(userid string, now time.Time) error {
	sqlStr := `UPDATE user SET lastlogintime = ? WHERE userid = ?`
	_, err := config.DB.Exec(sqlStr, now, userid)
	return err
}

// GetUserPassword 获取用户密码
func GetUserPassword(userid string) (string, error) {
	sqlStr := `SELECT password FROM user WHERE userid = ?`
	
	var password string
	err := config.DB.QueryRow(sqlStr, userid).Scan(&password)
	return password, err
}

// CheckUserExists 检查用户是否存在
func CheckUserExists(userid string) (bool, error) {
	sqlStr := `SELECT COUNT(*) FROM user WHERE userid = ?`
	
	var count int
	err := config.DB.QueryRow(sqlStr, userid).Scan(&count)
	if err != nil {
		return false, err
	}
	return count > 0, nil
}


